import sys
from flask import Response
from flask_restful import Resource
from src.formats import MapServiceManager

# 全局变量
mapServiceManager = MapServiceManager.mapServiceManager

sys.path.append("..")

class GetServicesList(Resource):
    def __init__(self):
        print('init services list')

    def get(self):
        resp = ''
        conTag = 'p'
        resp += 'Total Services：' + str(len(mapServiceManager.serviceList)) + '</br>' + '<hr>'
        for i in mapServiceManager.serviceList:

            resp += '<h2>' + i + '</h2>'
            resp += '<' + conTag + '>type：' + mapServiceManager.serviceList[i]['config']['type'] + '</' + conTag + '>'
            # resp += '<' + conTag + '>cnName：' + mapServiceManager.serviceList[i]['config']['cnName'] + '</' + conTag +'>'
            # resp += '<' + conTag + '>enName：' + mapServiceManager.serviceList[i]['config']['enName'] + '</' + conTag +'>'
            resp += '<' + conTag + '>serviceName：' + mapServiceManager.serviceList[i]['config']['serviceName'] + '</' + conTag +'>'
            resp += '<' + conTag + '>description：' + mapServiceManager.serviceList[i]['config']['describe'] + '</' + conTag +'>'
            resp += '<' + conTag + '>pkgPath：' + mapServiceManager.serviceList[i]['config']['pkgPath'] + '</' + conTag +'>'

            if mapServiceManager.serviceList[i]['config']['type'] == 'c3dtiles':
                resp += '<' + conTag + '>Cesium3DTilesUrl：' + '<a href=/tilemappkg/' + \
                        mapServiceManager.serviceList[i]['config']['serviceName'] + '/c3dtiles/scene.json>' + '/tilemappkg' + \
                        mapServiceManager.serviceList[i]['config'][
                            'serviceName'] + '/c3dtiles/scene.json' + '</a>' + '</' + conTag + '>'
            elif mapServiceManager.serviceList[i]['config']['type'] == 'tpk' or mapServiceManager.serviceList[i]['config']['type'] == 'mbt':
                resp += '<' + conTag + '>ESRIMapServerUrl：' + '<a href=/tilemappkg/' + mapServiceManager.serviceList[i]['config']['serviceName'] + '/MapServer>' + '/tilemappkg/' + mapServiceManager.serviceList[i]['config']['serviceName'] + '/MapServer' + '</a>' + '</' + conTag + '>'

            resp += '<hr>'

        resp = Response(resp, status=200, mimetype='text/html')
        return resp
